/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-25
 * Time: 01:37
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

using Labb4.Pooman.Cards;
using Labb4.Pooman.Players;
using Labb4.Pooman.Game;

namespace Labb4.Rules
{
	/// <summary>
	/// Performs the tasks "perform check for playability" and "perform deal cards" differently
	/// depending on what phase of the game we're in
	/// </summary>
	public class SetOfRulesPhase1 : SetOfRules
	{
		#region members
		
		private const int		m_NumberOfCardsToHold = 3;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, does nothing (neither does base, do I have to declare them then?)
		/// </summary>
		public SetOfRulesPhase1() :
			base()
		{
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// Attempts to deal a number of cards to the player sent as argument.
		/// The number of cards dealt is set in a local field
		/// </summary>
		/// <param name="player">Th eplayer to deal cards to</param>
		/// <param name="poo">Reference to the current instance of PoomanMain</param>
		public override void PerformDealCards(Player player, PoomanMain poo)
		{
			int delay  = 500;
			bool deckIsOut = false;
			
			while((player.CurrentHand.Cards.Count < m_NumberOfCardsToHold) && (!deckIsOut))
			{
				if(!poo.DealOneCardToPlayer(player, delay))
				{
					deckIsOut = true;
				}
				
				delay += delay;
			}
		}
		
		/// <summary>
		/// Checks what cards are playable. In this phase, all cards are playable if non are selected
		/// If a card is selected, only cards with the same value can be selected
		/// </summary>
		/// <param name="cards">The cards to check</param>
		/// <param name="round">Reference to the currently played round</param>
		/// <param name="trump">Reference to the current trump</param>
		/// <returns>The playable cards</returns>
		public override CardPile PerformCheckForPlayability(CardPile cards, Round round, Trump trump)
		{
			CardPile playable = new CardPile();
			int currentlySelected = 0;
			int valueOfSelected = 0;
			
			foreach(Card it in cards)
			{
				if(it.Selected)
				{
					currentlySelected++;
					playable.Add(it);
					valueOfSelected = it.ValueOfCard;
				}
			}
			
			if(currentlySelected == 0)
			{
				playable.Add(cards);
			}
			else
			{
				foreach(Card it in cards)
				{
					if(!it.Selected)
					{
						if(it.ValueOfCard == valueOfSelected)
						{
							playable.Add(it);
						}
					}
				}
			}
			
			return playable;
		}
		
		#endregion
	}
}
